package com.bmadmin.service.impl;

import com.bmadmin.domain.DataQueryItemPOJO;
import com.bmadmin.domain.DataQueryListPOJO;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.domain.PluginZtreeDataPOJO;
import com.bmadmin.mapper.DataQueryItemPOJODao;
import com.bmadmin.mapper.DataQueryListPOJODao;
import com.bmadmin.service.DataQueryManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 金玮良 on 2017-12-19.
 */
@Service
@Transactional
public class DataQueryManagementServiceImpl implements DataQueryManagementService {
    @Autowired
    DataQueryListPOJODao dataQueryListPOJODao;

    @Autowired
    DataQueryItemPOJODao dataQueryItemPOJODao;

    @Override
    public NormalResult getDataQueryByTree(DataQueryListPOJO dataQueryListPOJO) {
        NormalResult result = new NormalResult();

        List<PluginZtreeDataPOJO> pluginZtreeDataPOJOS = new ArrayList<PluginZtreeDataPOJO>();
        List<DataQueryListPOJO> dataQueryListPOJOS = dataQueryListPOJODao.find(dataQueryListPOJO);

        for(DataQueryListPOJO dataQueryListPOJOitem:dataQueryListPOJOS){
            PluginZtreeDataPOJO forAddQueryList = new PluginZtreeDataPOJO();

            forAddQueryList.setId(dataQueryListPOJOitem.getL_id());
            forAddQueryList.setPart_id(dataQueryListPOJOitem.getL_part_id());
            forAddQueryList.setName(dataQueryListPOJOitem.getL_id()+" "+ dataQueryListPOJOitem.getL_name());
            forAddQueryList.setOpen(true);
            forAddQueryList.setBooleanQuery(false);

            pluginZtreeDataPOJOS.add(forAddQueryList);
        }

        List<DataQueryItemPOJO> dataQueryItemPOJOSResult = dataQueryItemPOJODao.find(new DataQueryItemPOJO());

        for(DataQueryItemPOJO dataQueryItemPOJOItem:dataQueryItemPOJOSResult){
            PluginZtreeDataPOJO forAddQueryItem = new PluginZtreeDataPOJO();

            forAddQueryItem.setId(dataQueryItemPOJOItem.getQ_id()+"");
            forAddQueryItem.setPart_id(dataQueryItemPOJOItem.getQ_list_id());
            forAddQueryItem.setName(dataQueryItemPOJOItem.getQ_name());
            forAddQueryItem.setOpen(false);
            forAddQueryItem.setBooleanQuery(true);
            forAddQueryItem.setClick("getLineClick('" + dataQueryItemPOJOItem.getQ_id() + "')");

            pluginZtreeDataPOJOS.add(forAddQueryItem);
        }



        for(PluginZtreeDataPOJO sysDepartmentsPOJOItemOne:pluginZtreeDataPOJOS){
            List<PluginZtreeDataPOJO> forAddChildren = new ArrayList<PluginZtreeDataPOJO>();
            for(PluginZtreeDataPOJO sysDepartmentsPOJOItemTwo:pluginZtreeDataPOJOS){
                if(sysDepartmentsPOJOItemOne.getId().equals(sysDepartmentsPOJOItemTwo.getPart_id())){
                    forAddChildren.add(sysDepartmentsPOJOItemTwo);
                }
            }
            sysDepartmentsPOJOItemOne.setChildren(forAddChildren);
        }
        List<PluginZtreeDataPOJO> resultForLevelOne = new ArrayList<PluginZtreeDataPOJO>();
        for(PluginZtreeDataPOJO sysDepartmentsPOJOItem:pluginZtreeDataPOJOS){
            if("00".equals(sysDepartmentsPOJOItem.getPart_id())){
                resultForLevelOne.add(sysDepartmentsPOJOItem);
            }
        }

        result.setCode("0000");
        result.setMsg("查询成功");
        result.setObject(resultForLevelOne);

        return result;
    }

    @Override
    public NormalResult insertDataQueryList(DataQueryListPOJO dataQueryListPOJO) {
        NormalResult result = new NormalResult();

        DataQueryListPOJO dataQueryListPOJOForQueryId = new DataQueryListPOJO();
        dataQueryListPOJOForQueryId.setL_id(dataQueryListPOJO.getL_id());
        List<DataQueryListPOJO> dataQueryListPOJOSForQueryIdResult = dataQueryListPOJODao.find(dataQueryListPOJOForQueryId);
        if(dataQueryListPOJOSForQueryIdResult.size()>0){
            result.setCode("9999");
            result.setMsg("该清单id 已经存在");
            return result;
        }

        int resultForInsert = dataQueryListPOJODao.insertSelective(dataQueryListPOJO);

        if (resultForInsert == 1) {
            result.setCode("0000");
            result.setMsg("新增成功");
        } else {
            result.setCode("9999");
            result.setMsg("新增查询清单异常，请联系系统管理员");
        }
        return result;
    }

    @Override
    public NormalResult delDataQueryList(DataQueryListPOJO dataQueryListPOJO) {
        NormalResult result = new NormalResult();

        DataQueryListPOJO dataQueryListPOJOForQueryChildren = new DataQueryListPOJO();
        dataQueryListPOJOForQueryChildren.setL_part_id(dataQueryListPOJO.getL_id());

        List<DataQueryListPOJO> dataQueryListPOJOSQueriedByChildren = dataQueryListPOJODao.find(dataQueryListPOJOForQueryChildren);
        if(dataQueryListPOJOSQueriedByChildren.size()>0){
            result.setCode("9999");
            result.setMsg("该清单存在下级清单，不得进行删除");
            return result;
        }

        //TODO 如果存在下级查询，也不允许删除

        int resultForDelete = dataQueryListPOJODao.deleteByL_id(dataQueryListPOJO.getL_id());
        if (resultForDelete == 1) {
            result.setCode("0000");
            result.setMsg("删除成功");
        } else {
            result.setCode("9999");
            result.setMsg("删除查询清单数据异常，请联系系统管理员");
        }


        return result;
    }

    @Override
    public NormalResult updateDataQueryList(DataQueryListPOJO dataQueryListPOJO) {
        NormalResult result = new NormalResult();

        int resultForUpdate = dataQueryListPOJODao.update(dataQueryListPOJO);
        if (resultForUpdate == 1) {
            result.setCode("0000");
            result.setMsg("修改成功");
        } else {
            result.setCode("9999");
            result.setMsg("修改部门数据异常，请联系系统管理员");
        }

        return result;
    }

    @Override
    public NormalResult getDataQueryListById(DataQueryListPOJO dataQueryListPOJO) {
        return null;
    }
    
    public int getNextLid(String l_part_id){
    	return dataQueryListPOJODao.getNextLid(l_part_id);
    }
}
